package com.step.mesh;

public class Coordinate {
	float[] coordinates;
	
	public Coordinate(float x, float y){
		coordinates = new float[]{x, y};
	}
	
	public Coordinate(float x, float y,float z){
		coordinates = new float[]{x, y, z};
	}
	
	public int numbers() {
		return coordinates.length;
	}
	
	public void set(float x, float y, float z){
		coordinates[0] = x;
		coordinates[1] = y;
		
		if(coordinates.length == 3){			
			coordinates[2] = z;
		}
	}
	
	public void set(float x, float y){
		coordinates[0] = x;
		coordinates[1] = y;
	}
	
	public float getX(){
		return coordinates[0];
	}
	
	public float getY(){
		return coordinates[1];
	}
	
	public float getZ(){
		return coordinates.length == 3 ? coordinates[2] : 0;
	}
	
	public boolean is2D(){
		return coordinates.length == 2;
	}
	
	public boolean is3D(){
		return coordinates.length == 3;
	}
	
	public String toString() {
		String temp = "x="+coordinates[0]+",y="+coordinates[1];
		if(is3D()) temp+=",z="+coordinates[2];
		return temp;
	}
	
	public Coordinate clone(){
		if(is3D()){
			return new Coordinate(getX(), getY(), getZ());
		}
		else{
			return new Coordinate(getX(), getY());
		}
	}
	
	public static Coordinate subtract(Coordinate c1, Coordinate c2){
		Coordinate coordinate = null;
		float dx = c1.getX() - c2.getX();
		float dy = c1.getY() - c2.getY();
		
		if(c1.is2D() && c2.is2D()){
			coordinate = new Coordinate(dx,dy);
		}
		else if(c1.is3D() && c2.is3D()){
			float dz = c1.getZ() - c2.getZ();
			coordinate = new Coordinate(dx,dy,dz);
		}
		
		return coordinate;
	}
}


